<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use think\facade\Cache;
use think\facade\Db;

class Add extends Controller
{
    
    public function add_mycar()
    {
        // 当前用户信息
        $user = $this->getUser();
        $data = $this->request->post();
        if ($data['car_info1'] == '') {
            return $this->renderError('请上传车辆行驶证印章页');
        }
        if ($data['car_info2'] == '') {
            return $this->renderError('请上传车辆行驶证条码页');
        }
        if ($data['car_info3'] == '') {
            return $this->renderError('请上传车辆营运证');
        }
        if ($data['car_info4'] == '') {
            return $this->renderError('请上传车辆图片');
        }
        if ($data['addcar_type'] == '') {
            return $this->renderError('请选择车辆类型');
        }
        if ($data['addcar_plate'] == '') {
            return $this->renderError('请选择输入车牌号');
        }
        $check = Db::name('car')->where(array('driver_id'=>$data['distribution'],'is_delete'=>0))->find();
        if($check){
            return $this->renderError('此司机已绑定其他车辆！');
        }

        $inset = array(
            'user_id'    => $user['user_id'],
            'car_info1'  => $data['car_info1'],
            'car_info2'  => $data['car_info2'],
            'car_info3'  => $data['car_info3'],
            'car_info4'  => $data['car_info4'],
            'app_id'     => $data['app_id'],
            'car_type'   => $data['addcar_type'],
            'car_number' => $data['addcar_plate'],
            'driver_id'  => $data['distribution'],
            'category'   => 1,
            'create_time' => time(),
            'update_time' => time(),
        );

        $res = Db::name('car')->insertGetId($inset);
        if($res){
            return $this->renderSuccess('添加成功', []);
        }else{
            return $this->renderError('网络似乎开小差了', []);
        }
    }
    public function edit_mycar()
    {
        $data = $this->request->param();
        if ($data['car_info1'] == '') {
            return $this->renderError('请上传车辆行驶证印章页');
        }
        if ($data['car_info2'] == '') {
            return $this->renderError('请上传车辆行驶证条码页');
        }
        if ($data['car_info3'] == '') {
            return $this->renderError('请上传车辆营运证');
        }
        if ($data['car_info4'] == '') {
            return $this->renderError('请上传车辆图片');
        }
        if ($data['addcar_type'] == '') {
            return $this->renderError('请选择车辆类型');
        }
        if ($data['addcar_plate'] == '') {
            return $this->renderError('请选择输入车牌号');
        }
        $w[] = ['driver_id','=',$data['distribution']];
        $w[] = ['is_delete','=',0];
        $w[] = ['car_id','<>',$data['car_id']];
        $check = Db::name('car')
            ->where($w)
            ->find();
        if($check){
            return $this->renderError('此司机已绑定其他车辆！');
        }
        $inset = array(
            'car_info1'  => $data['car_info1'],
            'car_info2'  => $data['car_info2'],
            'car_info3'  => $data['car_info3'],
            'car_info4'  => $data['car_info4'],
            'app_id'     => $data['app_id'],
            'car_type'   => $data['addcar_type'],
            'car_number' => $data['addcar_plate'],
            'driver_id'  => $data['distribution'],
            'category'   => 1,
            'update_time' => time(),
        );
        $res = Db::name('car')->where(array('car_id'=>$data['car_id']))->update($inset);
        if($res){
            return $this->renderSuccess('编辑成功', ['car_id'=>$data['car_id']]);
        }else{
            return $this->renderError('网络似乎开小差了');
        }

    }
    public function add_usecar()
    {
        // 当前用户信息
        $user = $this->getUser();
        $data = $this->request->post();
        if ($data['car_info1'] == '') {
            return $this->renderError('请上传车辆行驶证印章页');
        }
        if ($data['car_info2'] == '') {
            return $this->renderError('请上传车辆行驶证条码页');
        }
        if ($data['car_info3'] == '') {
            return $this->renderError('请上传车辆营运证');
        }
        /*if ($data['car_info4'] == '') {
            return $this->renderError('请上传车辆图片');
        }*/
        if ($data['addcar_type'] == '') {
            return $this->renderError('请选择车辆类型');
        }
        if ($data['addcar_plate'] == '') {
            return $this->renderError('请选择输入车牌号');
        }

        $check = Db::name('car')->where(array('is_delete'=>0,'category'=>2,'car_number'=>$data['addcar_plate']))->find();
        if($check){
            return $this->renderError('已有此车牌号的二手车');
        }
        $inset = array(
            'user_id'    => $user['user_id'],
            'car_info1'  => $data['car_info1'],
            'car_info2'  => $data['car_info2'],
            'car_info3'  => $data['car_info3'],
            'app_id'     => $data['app_id'],
            'car_type'   => $data['addcar_type'],
            'car_number' => $data['addcar_plate'],
            'driver_id'  => $data['distribution'],
            'content'    => $data['product_introduce'],
            'price'      => $data['product_price'],
            'address'    => $data['product_address'],
            'brand_id'   => $data['product_type'],
            'journey'    => $data['product_journey'],
            'p_time'     => $data['product_time'],
            'mobile'    =>$data['mobile'],
            'discharge'  => $data['product_discharge'],
            'p_box'      => $data['product_box'],
            'transfer'   => $data['product_transfer'],
            'category'   => 2,
            'create_time' => time(),
            'update_time' => time(),
        );
        $res = Db::name('car')->insertGetId($inset);
        if($res){
            return $this->renderSuccess('添加成功', []);
        }else{
            return $this->renderError('网络似乎开小差了', []);
        }
    }
    public function add_driver()
    {
        // 当前用户信息
        $user = $this->getUser();
        $data = $this->request->post();
        if ($data['realname'] == '') {
            return $this->renderError('司机名字必须填写');
        }
        if ($data['address'] == '') {
            return $this->renderError('司机住址必须填写');
        }
        if ($data['driver_type'] == '') {
            return $this->renderError('司机驾照类型必须填写');
        }
        if ($data['driver_age'] == '') {
            return $this->renderError('司机驾龄必须填写');
        }
        // if ($data['my_mobile'] == '') {
        //     return $this->renderError('司机手机号必须填写');
        // }
        /*$check = Db::name('my_driver')->where(array('realname'=>$data['realname'],'user_id'=>$user['user_id'],'is_delete'=>0))->find();
        if($check){
            return $this->renderError('已有此司机');
        }*/
        $inset = array(
            'user_id'    => $user['user_id'],
            'realname'   => $data['realname'],
            //'mobile'     => $data['my_mobile'],
            'app_id'     => $data['app_id'],
            'age'        => $data['age'],
            'driver_age'   => $data['driver_age'],
            'driver_type'   => $data['driver_type'],
            'address'   => $data['address'],
            //'driver_t'   => 1,
            'create_time' => time(),
            'update_time' => time(),
        );
        $res = Db::name('my_driver')->insertGetId($inset);
        if($res){
            return $this->renderSuccess('添加成功');
        }else{
            return $this->renderError('网络似乎开小差了');
        }
    }
    public function add_bank()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        if ($data['bankName'] == '') {
            return $this->renderError('请填写持卡人姓名');
        }
        if ($data['bankCode'] == '') {
            return $this->renderError('请填写银行卡号');
        }
        if ($data['bankPhone'] == '') {
            return $this->renderError('请填写银行预留的手机号');
        }
        $where = array('user_id'=>$user['user_id'],'bank_code'=>$data['bankCode']);
        $bank = Db::name('user_bank')->where($where)->find();
        if (!empty($bank)) {
            return $this->renderError('银行卡已存在');
        }
        $inset = array(
            'user_id'    => $user['user_id'],
            'bank_uname' => $data['bankName'],
            'bank_code'  => $data['bankCode'],
            'mobile'     => $data['bankPhone'],
            // 'address'    => $data['address'],
            'create_time' => time(),
            'update_time' => time(),
        );
        $res = Db::name('user_bank')->insertGetId($inset);
        if($res){
            return $this->renderSuccess('添加成功', []);
        }else{
            return $this->renderError('网络似乎开小差了', []);
        }
    }
}